package com.forum.ajax.homepage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.forum.domain.Groups;
import com.forum.domain.GroupsDAO;
import com.forum.domain.Ucg;
import com.forum.domain.UcgDAO;
import com.forum.domain.User;
import com.forum.domain.UserDAO;

public class NewGroup extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public NewGroup() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html ; charset=utf-8");
		PrintWriter out = response.getWriter();

		try {
			// get parameters
			String name = request.getParameter("name");
			String userid = request.getParameter("id");
			String intro = request.getParameter("intro");
			String openess = request.getParameter("openess");

			UserDAO ud = new UserDAO();
			User user = ud.findById(Integer.parseInt(userid));

			// user concern groups number plus one
			user.setGroupcount(user.getGroupcount() + 1);

			GroupsDAO gd = new GroupsDAO();
			Groups group;
			
			List<Groups> groupslist = gd.findByProperty("name", name);
			if(groupslist.size() != 0)
			{
				out.print("error");
				return;
			}
			
			if (openess.equals("true"))
				group = new Groups(user, name, intro, new Date(
						System.currentTimeMillis()), true, 1);
			else
				group = new Groups(user, name, intro, new Date(
						System.currentTimeMillis()), false, 1);

			// should commit the group before new ucg
			gd.save(group);
			gd.getSession().beginTransaction().commit();

			// add ucg to user concern group table
			UcgDAO ucgdao = new UcgDAO();
			Ucg ucg = new Ucg(group, user);
			ucgdao.save(ucg);
			ucgdao.getSession().beginTransaction().commit();
			JSONObject groupJson = new JSONObject();
			groupJson.accumulate("id", group.getId());
			groupJson.accumulate("userNickname", group.getUser().getNickname());
			groupJson.accumulate("time", group.getBuilddate());
			out.print(groupJson);

		} catch (Exception e) {
			// if exception occurs then print error
			System.out.println("in");
			out.print("error");
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
